package com.wyd.System.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyd.System.Bean.Institute;
import com.wyd.System.Mapper.InstituteMapper;
import com.wyd.System.Service.InstituteService;
import com.wyd.handler.BusinessException;
import com.wyd.response.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 文永达
 * @since 2020-12-25
 */
@Service
public class InstituteServiceImpl extends ServiceImpl<InstituteMapper, Institute> implements InstituteService {

    @Autowired(required = false)
    private InstituteMapper instituteMapper;

    @Override
    public List<Institute> findInstitute() {
        return this.baseMapper.findInstitute();
    }

    @Override
    public IPage<Institute> findInstituteList(Page<Institute> page, QueryWrapper<Institute> wrapper) {
        return this.baseMapper.findInstituteList(page,wrapper);
    }


    @Override
    public void addInstitute(Institute institute) {
        //判断是否增加了同一个学院
        String instituteName = institute.getInstituteName();

        QueryWrapper<Institute> wrapper = new QueryWrapper<>();
        wrapper.eq("institute_name",instituteName);
        Integer count = this.baseMapper.selectCount(wrapper);
        if(count!=0){
            throw new BusinessException(ResultCode.INSTITUTE_ALREADY_EXIST.getCode(),
                    ResultCode.INSTITUTE_ALREADY_EXIST.getMessage());
        }
        //学院名没有对应的处理

        this.baseMapper.insert(institute);
    }

    @Override
    public void deleteInstituteById(Integer id) {
        Institute institute = instituteMapper.selectById(id);
        if(institute == null){
            throw new BusinessException(ResultCode.INSTITUTE_CAN_NOT_DELETE.getCode(),
                    ResultCode.INSTITUTE_CAN_NOT_DELETE.getMessage());
        }
        instituteMapper.deleteById(id);
    }
}
